package com.servlet;

import com.bean.User;
import com.service.UserService;
import com.service.impl.UserServiceImpl;
import email.SendEmaiUtil;

import javax.mail.MessagingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/EmailServlet.do")
public class EmailServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();

    public void sendEmail(HttpServletRequest request, HttpServletResponse response) throws MessagingException, ServletException, IOException {
        //获取请求的邮箱参数
        String email = request.getParameter("emails");
        User user = new User();
        user.setUseremail(email);
        User queryUseremail = userService.queryUseremail(user);
        if (queryUseremail == null) {
            request.setAttribute("msg", "邮箱错误或用户不存在");
            request.getRequestDispatcher("page/zhuce/returnPassword.jsp").forward(request, response);
        } else {
//            request.setAttribute("msg", "邮箱信息已发送哦");
            SendEmaiUtil.sendMail(queryUseremail.getUserName()+"你的密码是："+queryUseremail.getUserpassword(), request.getParameter("emails"));
            request.getRequestDispatcher("index1.jsp").forward(request, response);
        }


    }
}